package org.foreveross.hbase.client;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fare.taluo.db.hbase.HBaseCellModel;
import com.fare.taluo.db.hbase.HBaseRowModel;
import com.fare.taluo.vo.crawl.statistics.StaticHistoryHbaseVO;

public class HbaseStaticHistoryHbaseHelper {
	private static Map<String,Method> staticHistoryMethods = new HashMap<String, Method>();
	
	static{
		Method[] HistoryMeths = StaticHistoryHbaseVO.class.getMethods();
		Class<?>[] clazz = null;
		Class<?> clz = null;
		for (Method m : HistoryMeths) {
			clazz = m.getParameterTypes();
			if(m.getName().startsWith("set") && clazz != null && clazz.length == 1){
				clz = clazz[0];
				if(!Collection.class.isAssignableFrom(clz)) //非设置集合的
					staticHistoryMethods.put(m.getName(), m);
			}
		}
	}
	
	public static List<StaticHistoryHbaseVO> modelTranEntity(List<HBaseRowModel> rowModel) throws Exception{
		String qualifier,value,family;
		List<StaticHistoryHbaseVO> voList=new ArrayList<StaticHistoryHbaseVO>();
		for (HBaseRowModel r : rowModel) {
			StaticHistoryHbaseVO vo=new StaticHistoryHbaseVO();
			Set<HBaseCellModel> cells=r.getCells();
			for(HBaseCellModel cell:cells){
				family=cell.getFamily();
				qualifier=cell.getQualifier();
				value = new String(cell.getValue().getBytes(),"UTF-8");
				if("crawlInfos".equals(family)){
					Method method =staticHistoryMethods.get("set" + qualifier.substring(0, 1).toUpperCase()+ qualifier.substring(1));
					if(null != method){
						method.invoke(vo, value);
					}
				}
			}
		/*	for (KeyValue kv : r.raw()) {
				qualifier = new String(kv.getQualifier(),"utf-8");
				family = new String(kv.getFamily(),"utf-8");
				value = new String(kv.getValue(), "utf-8");
				if("crawlInfos".equals(family)) {//所需要的字段内容在crawlInfos可以找到
					Method method =staticHistoryMethods.get("set" + qualifier.substring(0, 1).toUpperCase()+ qualifier.substring(1));
					if(null != method){
						method.invoke(vo, value);
					}
				}
			}*/
			voList.add(vo);
			
		}
		return voList;
	}
}
